package com.example.springbootdemo.Controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.example.springbootdemo.Service.IBannerService;
import com.example.springbootdemo.entity.Banner;
import com.example.springbootdemo.utils.ApiResult;
//import org.springframework.security.access.prepost.PreAuthorize;
import com.example.springbootdemo.utils.PageResult;
import com.example.springbootdemo.utils.PassToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * bannersController
 * 
 * @author ruoyi
 * @date 2025-03-27
 */
@RestController
@RequestMapping("/system/banner")
public class BannerController
{
    @Autowired
    private IBannerService bannerService;

    /**
     * 查询banners列表
     */
    @PassToken   //给通过令牌
    @GetMapping("/list")
    public ApiResult list(Banner banner,int pageNum,int pageSize)
    {
        PageResult pageResult  = bannerService.selectBannerList(banner,pageNum,pageSize);
        return ApiResult.createBySuccess(200,pageResult,"获取成功");
    }

    /**
     * 导出banners列表
     */
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Banner banner)
//    {
//        List<Banner> list = bannerService.selectBannerList(banner);
//        ExcelUtil<Banner> util = new ExcelUtil<Banner>(Banner.class);
//        util.exportExcel(response, list, "banners数据");
//    }

    /**
     * 获取banners详细信息
     */
    @GetMapping(value = "/{id}")
    public ApiResult getInfo(@PathVariable("id") Long id)
    {
            return ApiResult.createBySuccess(200,bannerService.selectBannerById(id),"获取成功");

    }

    /**
     * 新增banners
     */

    @PostMapping
    public ApiResult add(@RequestBody Banner banner)
    {
        int i = bannerService.insertBanner(banner);
        if(i>0){
            return ApiResult.createBySuccess(200,"","添加成功");
        }else{
            return ApiResult.createByFail(500,"添加失败");
        }
    }

    /**
     * 修改banners
     */
    @PutMapping
    public ApiResult edit(@RequestBody Banner banner)
    {
        int i = bannerService.updateBanner(banner);
        if(i>0){
            return ApiResult.createBySuccess(200,"","修改成功");
        }else{
            return ApiResult.createByFail(500,"修改成功");
        }
    }

    /**
     * 删除banners
     */

	@DeleteMapping("/{ids}")
    public ApiResult remove(@PathVariable Long[] ids)
    {
        int i = bannerService.deleteBannerByIds(ids);
            if(i>0){
                return ApiResult.createBySuccess(200,"","删除成功");
            }else{
                return ApiResult.createByFail(500,"删除成功");
            }
    }
}
